package com.wgchao.authmanager.application.service;

import com.wgchao.authmanager.application.vo.RoleVo;
import com.wgchao.authmanager.domain.model.Administrator;
import com.wgchao.authmanager.domain.params.AdministratorCreator;
import com.wgchao.authmanager.domain.params.AdministratorUpdator;
import com.wgchao.authmanager.domain.model.Agency;
import com.wgchao.authmanager.domain.model.Role;
import com.wgchao.authmanager.domain.model.RoleIdentity;
import com.wgchao.authmanager.domain.model.SetRoleWithAdminDomainService;
import com.wgchao.authmanager.domain.model.UserRole;
import com.wgchao.authmanager.domain.repository.AdministratorRepository;
import com.wgchao.authmanager.domain.repository.AgencyRepository;
import com.wgchao.authmanager.domain.repository.RoleRepository;
import com.wgchao.authmanager.domain.repository.UserRoleRepository;
import com.wgchao.authmanager.infrastructure.exception.ObjectDisabledException;
import com.wgchao.authmanager.infrastructure.exception.ObjectNotExistException;
import com.wgchao.authmanager.infrastructure.exception.ObjectNotLegalException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * @ClassName: AdministratorService
 * @author: wgchao
 * @createTime: 2020/4/26 10:37 AM
 * @Description: 账户（应用服务）
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class AdministratorService {
    @Autowired
    private AdministratorRepository administratorRepository;
    @Autowired
    private AgencyRepository agencyRepository;
    @Autowired
    private RoleRepository roleRepository;
    @Autowired
    private UserRoleRepository userRoleRepository;
    @Autowired
    private SetRoleWithAdminDomainService setRoleWithAdminDomainService;

    /**
     * 向机构添加新的管理员
     * @param agencyCode
     * @param creator
     * @param operator
     */
    public void addAdmin(String agencyCode, AdministratorCreator creator, String operator){
        Agency agency = agencyRepository.get(agencyCode);
        if(agency == null){
            throw new ObjectNotExistException("所在机构不存在");
        }
        String encryPwd = administratorRepository.encryptePwd(creator.getPassword());
        Administrator administrator = Administrator.addAdmin(creator, encryPwd, agency, operator);
        administratorRepository.add(administrator);
    }

    /**
     * 修改管理员信息
     * @param uname
     * @param updator
     * @param operator
     */
    public void resiveAdminInfo(String uname, AdministratorUpdator updator, String operator){
        Administrator administrator = administratorRepository.get(uname);
        if(administrator == null){
            throw new ObjectNotExistException("账户不存在");
        }
        administrator.resiveInfo(updator, operator);
        administratorRepository.add(administrator);
    }

    /**
     * 停用管理员账户
     * @param uname
     * @param operator
     */
    public void stopUsingAdmin(String uname, String operator){
        Administrator administrator = administratorRepository.get(uname);
        if(administrator == null){
            throw new ObjectNotExistException("账户不存在");
        }
        administrator.stopUsing(operator);
        administratorRepository.add(administrator);
    }

    /**
     * 重新启用管理员账户
     * @param uname
     * @param operator
     */
    public void reUseAdmin(String uname, String operator){
        Administrator administrator = administratorRepository.get(uname);
        if(administrator == null){
            throw new ObjectNotExistException("账户不存在");
        }
        administrator.reEnable(operator);
        administratorRepository.add(administrator);
    }

    /**
     * 删除管理员
     * @param uname
     * @param operator
     */
    public void removeAdmin(String uname, String operator){
        Administrator administrator = administratorRepository.get(uname);
        if(administrator == null){
            throw new ObjectNotExistException("账户不存在");
        }
        administratorRepository.remove(administrator, operator);
    }


    /**
     * 为账户设置角色
     * @param uname
     * @param roleNames
     * @param operator
     */
    public void setRolesToAdmin(String uname, List<String> roleNames, String operator){
        Administrator administrator = administratorRepository.get(uname);
        if(administrator == null){
            throw new ObjectNotExistException("账户不存在");
        }
        List<Role> roles = new ArrayList<>();
        for(String roleName : roleNames){
            Role role = roleRepository.get(new RoleIdentity(administrator.getAgencyCode(), roleName));
            if(role == null){
                throw new ObjectNotExistException(roleName+"角色不存在");
            }
            roles.add(role);
        }
        UserRole userRole = setRoleWithAdminDomainService.setRolesToAdmin(administrator, roles, operator);
        userRoleRepository.add(userRole);
    }


}
